package com.central.apps.controller;

import com.central.app.common.util.DateUtils;
import com.central.app.service.HandlerService;
import com.central.apps.common.util.DateUtiles;
import com.central.apps.handler.purchase.PurchaseReceiptHandler;
import com.central.apps.service.IInventoryService;
import com.central.common.web.Result;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.MARSHAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.time.chrono.IsoChronology;
import java.util.*;

/**
 * 库存管理控制类
 *
 * @author 李大超
 * @date 2022/8/23
 */
@RestController
@Api(tags = "库存接口类")
@Slf4j
@RequestMapping("/inventory")
public class InventoryController {

    @Autowired
    private HandlerService handlerService;
    @Autowired
    private IInventoryService inventoryService;
    @Autowired
    private PurchaseReceiptHandler purchaseReceiptHandler;

    @PostMapping("/setBatch")
    public String setBatch(String ids, String isenabled) throws IOException {
        JSONObject json = new JSONObject();
        json.put("code", "0");
        json.put("msg", "批次设置成功");

        if (StringUtils.isEmpty(ids)) {
            return json.toString();
        }

        // 1.更新物料批次设置
        handlerService.updateBySql("update gq_item set enablebatch = '" + isenabled + "' where id in (" + ids + ");");
        return json.toString();
    }

    @PostMapping("/modifyQuantity")
    public String modifyQuantity(HttpServletRequest request, @RequestBody Map<String, Object> params) throws Exception {
        JSONObject json = new JSONObject();
        json.put("code", "0");
        json.put("msg", "调整库存余量成功");

        if (null == params) {
            return json.toString();
        }

        String id = MapUtils.getString(params, "id");
        float curbal = MapUtils.getFloat(params, "curbal");
        String remarks = MapUtils.getString(params, "remarks");
        String adjustby = MapUtils.getString(params, "adjustby");
        List<Map<String, Object>> list = handlerService.findBySql("select * from gq_invbalances where id =" + id);
        if (CollectionUtils.isNotEmpty(list)) {
            params = list.get(0);
            String storenum = MapUtils.getString(params, "storenum");
            String itemnum = MapUtils.getString(params, "itemnum");
            String binnum = MapUtils.getString(params, "binnum");
            String batchnum = MapUtils.getString(params, "batchnum");
            String taskid = MapUtils.getString(params, "taskid");
            String proid = MapUtils.getString(params, "proid");
            String updateuserdesc = MapUtils.getString(params, "updateuserdesc");
            String adjustdate = DateUtils.dateToStr(new Date(),"yyyy-MM-dd HH:ss");//MapUtils.getString(params, "adjustdate");
            float oldcurbal = MapUtils.getFloat(params, "curbal");

            // 更新库存余量
//            handlerService.updateBySql("update gq_invbalances set curbal = " + curbal +
//                    " where itemnum = '" + itemnum + "' and storenum = '" + storenum
//                    + "' and binnum = '" + binnum + "' and batchnum = '" + batchnum + "' and taskid ='" + taskid + "' and proid='" + proid + "'");
            handlerService.updateBySql("update gq_invbalances set curbal = " + curbal + " where id = '" + id + "'");
            //List<Map<String, Object>> bySql = handlerService.findBySql("select avblbalance,reservedqty from gq_invbalances where itemnum = '" + itemnum + "' and storenum = '" + storenum + "' and binnum = '" + binnum + "' and batchnum = '" + batchnum + "' and taskid ='" + taskid + "' and proid='" + proid + "'");
            List<Map<String, Object>> bySql = handlerService.findBySql("select avblbalance,reservedqty from gq_invbalances where id = '" + id + "'");

            if (CollectionUtils.isNotEmpty(bySql)) {
                Float avblbalance = MapUtils.getFloat(bySql.get(0), "avblbalance");
                Float reservedqty = MapUtils.getFloat(bySql.get(0), "reservedqty");
                float avblbalance1 = curbal - reservedqty;
//                handlerService.updateBySql("update gq_invbalances set avblbalance = " + avblbalance1 +
//                        ", adjusteduser = '" + updateuserdesc + "', adjustdate = '" + adjustdate + "' , adjustdesc='" + remarks +
//                        "' where itemnum = '" + itemnum + "' and storenum = '" + storenum
//                        + "' and binnum = '" + binnum + "' and batchnum = '" + batchnum + "' and taskid ='" + taskid + "' and proid='" + proid + "' ");
                handlerService.updateBySql("update gq_invbalances set avblbalance = " + avblbalance1 +
                        ", adjusteduser = '" + adjustby + "', adjustdate = '" + adjustdate+ "' , adjustdesc='" + remarks +
                        "' where id = '" + id + "'");

                // 更新库存信息
                inventoryService.calInvQuantity(storenum, itemnum);
                float quantity = curbal - oldcurbal;
                // 更新库存调整记录
                Map<String, Object> updMap = new HashMap<>();
                //根据批次查库存成本
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select avgcost from gq_invcost where batchnum  = '" + batchnum + "'");
                if(CollectionUtils.isNotEmpty(bySql1)){
                    Map<String, Object> map = bySql1.get(0);
                    String avgcost = MapUtils.getString(map, "avgcost");
                    BigDecimal bigDecimal = new BigDecimal(avgcost);
                    updMap.put("oldcost", bigDecimal.toString());//原库存成本
                    updMap.put("newcost", bigDecimal.toString());//当前库存成本
                }
                Date date = new Date();
                SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                updMap.put("storenum", storenum);
                updMap.put("itemnum", itemnum);
                updMap.put("binnum", binnum);
                updMap.put("batchnum", batchnum);
                updMap.put("adjustby", adjustby);
                updMap.put("adjustdate",sdf.format(date));
                updMap.put("oldcurbal", oldcurbal);
                updMap.put("curbal", curbal);
                updMap.put("quantity", quantity);
                updMap.put("remarks", remarks);
                updMap.put("taskid", taskid);
                updMap.put("proid", proid);
                handlerService.insert("gq_invadjustment", updMap);
            }
        }


        return json.toString();
    }

    @PostMapping("/modifyCost")
    public String modifyStdCost(HttpServletRequest request, @RequestBody Map<String, Object> params) throws Exception {
        JSONObject json = new JSONObject();
        json.put("code", "0");
        json.put("msg", "调整库存平均成本成功");
        String id = MapUtils.getString(params, "id");
        String avgcost = MapUtils.getString(params, "avgcost");
        BigDecimal bigDecimal1 = new BigDecimal(avgcost);
        String remarks = MapUtils.getString(params, "remarks");
        String adjustby = MapUtils.getString(params, "adjustby");
        String adjustdate = DateUtils.dateToStr(new Date(),"yyyy-MM-dd HH:ss");
        List<Map<String, Object>> list = handlerService.findBySql("select * from gq_invcost where id =" + id);
        if (null == params) {
            return json.toString();
        }
        if (CollectionUtils.isNotEmpty(list)) {
            params = list.get(0);
            String storenum = MapUtils.getString(params, "storenum");
            String itemnum = MapUtils.getString(params, "itemnum");
            String batchnum = MapUtils.getString(params, "batchnum");
            String updateuserdesc = MapUtils.getString(params, "updateuserdesc");
            String updatetime =  DateUtils.dateToStr(new Date(),"yyyy-MM-dd HH:ss");//MapUtils.getString(params, "adjustdate");
            String oldavgcost = MapUtils.getString(params, "avgcost");
            BigDecimal oldavgcost1 = new BigDecimal(oldavgcost);
            float stdcost = MapUtils.getFloat(params, "stdcost");
            String taskid = MapUtils.getString(params, "taskid");
            String proid = MapUtils.getString(params, "proid");
//        float oldAvgcost = MapUtils.getFloat(params,"oldAvgcost");

            // 更新库存标准成本
            handlerService.updateBySql("update gq_invcost set  avgcost = " + bigDecimal1 +
                    ", updateuser = '" + updateuserdesc + "', updatetime = '" + adjustdate+ "'" +
                    " where id = '" + id + "' ");
            handlerService.updateBySql("update gq_invbalances set unitcost="+bigDecimal1+" where batchnum='"+batchnum+"'");
            // 更新库存调整记录
            Map<String, Object> updMap = new HashMap<>();
            //根据批次查库存余量
            List<Map<String, Object>> bySql = handlerService.findBySql("select curbal from gq_invbalances where batchnum = '" + batchnum + "'");
            if(CollectionUtils.isNotEmpty(bySql)){
                Map<String, Object> map = bySql.get(0);
                String curbal = MapUtils.getString(map, "curbal");
                BigDecimal bigDecimal = new BigDecimal(curbal);
                updMap.put("oldcurbal", bigDecimal.toString());//原库存余量
                updMap.put("curbal", bigDecimal.toString());//当前余量
            }
            Date date = new Date();
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            updMap.put("itemnum", itemnum);
            updMap.put("storenum", storenum);
            updMap.put("batchnum", batchnum);
            updMap.put("adjustby", adjustby);
            updMap.put("adjustdate", sdf.format(date));
            updMap.put("oldcost", oldavgcost1);
            updMap.put("newcost", bigDecimal1);
            updMap.put("remarks", remarks);
            updMap.put("taskid", taskid);
            updMap.put("proid", proid);
            handlerService.insert("gq_invadjustment", updMap);

        }
        return json.toString();
    }


    /**
     * 扫码入库
     */
    @PostMapping("/scanCode")
    public Map<String, Object> scanCodeForWarehouse(@RequestBody List<Map<String, Object>> params) throws Exception {
        HashMap<String, String> hashMap2 = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        String code = "9999";
        String message = "";
        String idt = "";
        String receiptnum = "";
        HashSet<Long> prtidSet = new HashSet<>();//存放采购接收id的set
        for (Map<String, Object> map : params) {
            String storenum = MapUtils.getString(map, "storenum");//
            String shelfnum = MapUtils.getString(map, "shelfnum");//
            List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("children");
            for (Map<String, Object> map2 : list) {
                float numbers = 0;
                receiptnum = MapUtils.getString(map2, "receiptnum");
                log.info("receiptnum+++++++++++++++++++++++++++++++++++++" + receiptnum);
                String itemnum = MapUtils.getString(map2, "itemnum");
                String batchnum = MapUtils.getString(map2, "batchnum");//批次

                //根据物料编码，批次查编码表里，判断这个码是否扫过了
                List<Map<String, Object>> bySql7 = handlerService.findBySql("select isscancode from gq_barcode where itemnum = '" + itemnum + "' and batchnum = '" + batchnum + "' order by createtime desc");
                if(CollectionUtils.isNotEmpty(bySql7)){
                    Map<String, Object> map1 = bySql7.get(0);
                    String isscancode = MapUtils.getString(map1, "isscancode");
                    if("1".equals(isscancode)){
                        //证明码已经扫了提示
                        message = "物料编码为" + itemnum + "的物料生成的条码已经扫码入库了，不能重复扫描";
                        result.put("code", "0");
                        result.put("size", params.size());
                        result.put("msg", message);
                        return result;
                    }
                }

                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_prtdetailline where itemnum ='" + itemnum + "' and batchnum = '" + batchnum +"'");
                if(CollectionUtils.isEmpty(bySql)){
                    message = "根据批次号和物料编号没有查到对应的采购接受行。";
                    result.put("code", "0");
                    result.put("size", params.size());
                    result.put("msg", message);
                    return result;
                }
                Map<String, Object> map6 = bySql.get(0);
                String prtid = MapUtils.getString(map6, "prtid");//采购接收id
                String itemdesc = MapUtils.getString(map6, "itemdesc");//物料描述
                String receiveqty = MapUtils.getString(map6, "receiveqty");//采购接受，接收数量
                String subjectno = MapUtils.getString(map6, "subjectno");//课题号
                String applyby = MapUtils.getString(map6, "applyby");//需求人--占有人
                String occupnot = MapUtils.getString(map6, "occupnot");
                String prtslid = MapUtils.getString(map6, "prtslid");//采购会总行id
                Long prtdetaillineid = MapUtils.getLong(map6, "id");//采购接受行id
                String prlid = MapUtils.getString(map6, "prlid");//采购申请行id
                String pplid = MapUtils.getString(map6, "pplid");//采购计划行id
                String polid = MapUtils.getString(map6, "polid");//采购订单明细行id
                for (Map<String, Object> map3 : bySql) {
                    String sonitemnum = MapUtils.getString(map3, "itemnum");
                    String msg;
                    if (itemnum.equals(sonitemnum)) {
                        List<Map<String, Object>> bySql1 = handlerService.findBySql("select storedesc from gq_store where storenum='" + storenum + "'");
                        String storedesc = MapUtils.getString(bySql1.get(0), "storedesc");
                        handlerService.updateBySql("update gq_prtdetailline set warehousenum ='" + storenum + "',warehousedesc='"+storedesc+"',binnum ='" + shelfnum + "' where prtid ='" + prtid + "' and itemnum ='" + itemnum + "'");
                        msg = ("采购接收号:" + receiptnum + ",\n物料编码:" + itemnum + ",\n批次号:" + batchnum + ",\n更新成功");
                        hashMap2.put("storenum", storenum);
                        hashMap2.put("shelfnum", shelfnum);
                        hashMap2.put("itemnum", itemnum);
                        hashMap2.put("batchnum", batchnum);
                        hashMap2.put("receiptnum", receiptnum);
                    } else {
                        msg = ("采购接收号:" + receiptnum + ",\n物料编码:" + itemnum + ",\n批次号:" + batchnum + ",\n更新失败");
                    }
                    message = message + msg + "\n";
                }
                //扫码入库
                //1.根据itemnum和storenum判断库存台账是否有此条数据
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_inventory where itemnum ='" + itemnum + "' and storenum ='" + storenum + "'");
                if (bySql1 == null || bySql1.size() == 0) {
                    //库存台账不存在此条数据,新建库存台账
                    Map<String, Object> map1 = new HashMap<>();
                    map.put("itemnum", itemnum);
                    map.put("itemdesc", itemdesc);
                    List<Map<String, Object>> bySql2 = handlerService.findBySql("select storedesc from gq_store where storenum ='" + storenum + "'");
                    if(CollectionUtils.isNotEmpty(bySql2)){
                        String storedesc = MapUtils.getString(bySql2.get(0), "storedesc");
                        map.put("storedesc", storedesc);
                    }
                    map.put("storenum", storenum);
                    map.put("binnum", shelfnum);
                    map.put("curbaltotal",receiveqty);
                    map.put("subjectno",subjectno);
                    BigInteger id = (BigInteger) handlerService.insert("gq_inventory", map);

                    List<Map<String, Object>> bySql3 = handlerService.findBySql("select * from gq_prtdetailline where prtid ='" + prtid + "' and itemnum ='" + itemnum + "' and warehousenum ='" + storenum + "' and batchnum ='" + batchnum + "'");
                    if (bySql3 != null && bySql3.size() > 0) {
                        for (Map<String, Object> map3 : bySql3) {
                            String itemtype = MapUtils.getString(map3, "itemtype");
                            map.put("itemtype", itemtype);
                            String outunit = MapUtils.getString(map3, "outunit");
                            map.put("orderunit", outunit);
                            map.put("outunit", outunit);
                            String model = MapUtils.getString(map3, "model");
                            map.put("model", model);
                            String spemodel = MapUtils.getString(map3, "spemodel");
                            map.put("spemodel", spemodel);
                            String manufacturer = MapUtils.getString(map3, "manufacturer");
                            map.put("manufacturer", manufacturer);
                            map.put("id", id);
                            List<Map<String, Object>> bySql4 = handlerService.findBySql("select * from gq_item where itemnum ='" + itemnum + "' limit 1");
                            map.put("orderunit", MapUtils.getString(bySql4.get(0), "unit"));
                            map.put("specifications", MapUtils.getString(bySql4.get(0), "specifications"));//规格
                            map.put("outunit", MapUtils.getString(bySql4.get(0), "outunit"));
                            map.put("packagform", MapUtils.getString(bySql4.get(0), "packform"));
                            map.put("moneylife", MapUtils.getString(bySql4.get(0), "lifemoney"));
                            map.put("quagrade", MapUtils.getString(bySql4.get(0), "qualitygrade"));
                            map.put("maintechindicat", MapUtils.getString(bySql4.get(0), "maintechtors"));
                            handlerService.update("gq_inventory", map);
                            //新建库存台账 成本子表1
                            numbers = InsertInventoryAllowanceCost(receiptnum, storenum, itemnum, batchnum, itemdesc, subjectno, applyby,occupnot, map3, shelfnum);
                        }
                    }
                } else {
                    //获取接收子表数量,添加到库存台账 余量表
                    List<Map<String, Object>> bySql3 = handlerService.findBySql("select * from gq_prtdetailline where id ='" + prtdetaillineid + "'");
                    if(CollectionUtils.isNotEmpty(bySql3)){
                        numbers = MapUtils.getInteger(bySql3.get(0), "receiveqty");//入库数量
                    }
                    numbers = InsertInventoryAllowanceCost(receiptnum, storenum, itemnum, batchnum, itemdesc, subjectno, applyby,occupnot, map6, shelfnum);
                    //往库存成本表，库存余量插数据
//                    handlerService.findBySql("insert into gq_invcost (proid,taskid,stdcost,avgcost,lastcost,itemnum,itemdesc,storenum,batchnum,subjectno) values " +
//                            "('" + MapUtils.getString(map6, "proid") + "','" + MapUtils.getString(map6, "taskid") + "','"
//                            +MapUtils.getString(map6, "unitcost") + "','" + MapUtils.getString(map6, "unitcost") + "','"
//                            +MapUtils.getString(map6, "linecost") + "','" + itemnum + "','" + itemdesc + "','" + storenum + "','" + batchnum + "','" + subjectno + "')");
//
//                    String expirydate = MapUtils.getString(map6, "expirydate");
//                    if(StringUtils.isNotBlank(expirydate)){
//                        handlerService.findBySql("insert into gq_invbalances (proid,taskid,avblbalance,curbal,enddate,unitcost,itemnum,itemdesc,orderunit,storenum,batchnum,receiptnum,subjectno,applyby,binnum) values " +
//                                "('" + MapUtils.getString(map6, "proid") + "','" + MapUtils.getString(map6, "taskid") + "','"
//                                +MapUtils.getString(map6, "receiveqty") + "','" + MapUtils.getString(map6, "receiveqty") + "','"
//                                +expirydate + "','" + MapUtils.getString(map6, "unitcost") + "','"
//                                + itemnum + "','" + itemdesc + "','" + MapUtils.getString(map6, "orderunit") + "','" + storenum + "','"
//                                +batchnum + "','" + receiptnum + "','" + subjectno + "','" + applyby + "','" + MapUtils.getString(map6, "binnum") + "')");
//                    }else{
//                        handlerService.findBySql("insert into gq_invbalances (proid,taskid,avblbalance,curbal,unitcost,itemnum,itemdesc,orderunit,storenum,batchnum,receiptnum,subjectno,applyby,binnum) values " +
//                                "('" + MapUtils.getString(map6, "proid") + "','" + MapUtils.getString(map6, "taskid") + "','"
//                                +MapUtils.getString(map6, "receiveqty") + "','" + MapUtils.getString(map6, "receiveqty") + "','"
//                                + MapUtils.getString(map6, "unitcost") + "','"
//                                + itemnum + "','" + itemdesc + "','" + MapUtils.getString(map6, "orderunit") + "','" + storenum + "','"
//                                +batchnum + "','" + receiptnum + "','" + subjectno + "','" + applyby + "','" + MapUtils.getString(map6, "binnum") + "')");
//                    }

//                    List<Map<String, Object>> bySq4 = handlerService.findBySql("select * from gq_invbalances where itemnum ='" + itemnum + "' and storenum ='" + storenum + "' and batchnum ='" + batchnum + "'");
//                    if (bySq4 != null && bySq4.size() > 0) {
//                        Integer avblbalance = MapUtils.getInteger(bySq4.get(0), "avblbalance");
//                        Integer curbal = MapUtils.getInteger(bySq4.get(0), "curbal");
//                        avblbalance = avblbalance + numbers;
//                        curbal = curbal + numbers;
////                        handlerService.updateBySql("update gq_invbalances set avblbalance =" + avblbalance + ",curbal =" + curbal + " where itemnum ='" + itemnum + "' and storenum ='" + storenum + "' and batchnum ='" + batchnum + "'");
//                        //新建成本表
//                        //handlerService.update("insert into gq_invcost (proid,taskid,batchnum,stdcost,avgcost,lastcost,storenum) values ('"++"','"++"','"++"','"++"','"++"','"++"','"++"','"++"')");
//                    }
                }
                List<Map<String, Object>> bySql4 = handlerService.findBySql("select avblbalance,curbal,reservedqty from gq_invbalances where itemnum ='" + itemnum + "' and storenum ='" + storenum + "'");
                float curbaltotal = 0;
                float avblbalancetotal = 0;
                float reservedqtytotal = 0 ;
                if(CollectionUtils.isNotEmpty(bySql4)){
                    for (Map<String, Object> map3 : bySql4) {
//                        float count=0;
                        Float avblbalance = MapUtils.getFloat(map3, "avblbalance");
                        Float reservedqty = MapUtils.getFloat(map3, "reservedqty");
//                        if("否".equals(occupnot)){
//                            count = MapUtils.getFloat(map3, "avblbalance");
//                        }else{
//                            count = MapUtils.getFloat(map3, "reservedqty");
//                        }
                        Integer curbal = MapUtils.getInteger(map3, "curbal");
                        curbaltotal = curbaltotal + curbal;
                        avblbalancetotal = avblbalancetotal + avblbalance;
                        reservedqtytotal = reservedqtytotal + reservedqty;
                    }
                }
                //是本人占用   变更预留数量，不是本人占用  变更可用余量

                    handlerService.updateBySql("update gq_inventory set curbaltotal =" + curbaltotal + ",reservedqty = " +reservedqtytotal+ ",avblbalance =" + avblbalancetotal + " where itemnum ='" + itemnum + "' and storenum ='" + storenum + "'");

//                    handlerService.updateBySql("update gq_inventory set curbaltotal =" + curbaltotal + ",reservedqty =" + avblbalancetotal + " where itemnum ='" + itemnum + "' and storenum ='" + storenum + "'");

                //新建入库记录
                Map<String, Object> map1 = new HashMap<>();
                List<Map<String, Object>> bySql2 = handlerService.findBySql("select instonum from gq_matusetrans where type in('接收','退回') and instonum like '%CDFI%' order by instonum desc limit 1");
                if(CollectionUtils.isNotEmpty(bySql2)){
                    String instonum = MapUtils.getString(bySql2.get(0), "instonum");
                    int instonumin = Integer.valueOf(instonum.substring(instonum.length() - 8));
                    instonumin++;
                    instonum = "CDFI"+instonumin;
//                    Integer instonum = MapUtils.getInteger(bySql2.get(0), "instonum");
//                    if(instonum!=null){
//                        instonum++;
//                    }
                    map1.put("instonum", instonum);
                }else{
                    map1.put("instonum", "CDFI10000001");
                }
                map1.put("instodesc", receiptnum + "接收入库");
                map1.put("itemnum", itemnum);
                map1.put("itemdesc", itemdesc);
                map1.put("storenum", storenum);
                List<Map<String, Object>> bySql3 = handlerService.findBySql("select storedesc from gq_store where storenum ='" + storenum + "'");
                if(CollectionUtils.isNotEmpty(bySql3)){
                    String storedesc = MapUtils.getString(bySql3.get(0), "storedesc");
                    map1.put("storedesc", storedesc);
                }
                List<Map<String, Object>> bySql6 = handlerService.findBySql("select * from gq_prtdetailline where id ='" + prtdetaillineid + "'");
                if(CollectionUtils.isNotEmpty(bySql6)){
                    BigDecimal unitcost = new BigDecimal(MapUtils.getString(bySql6.get(0), "unitcost"));
                    BigDecimal reamount = new BigDecimal(MapUtils.getString(bySql6.get(0), "reamount"));
//                    Float unitcost = MapUtils.getFloat(bySql6.get(0), "unitcost");
//                    Float linecost = MapUtils.getFloat(bySql6.get(0), "linecost");
                    String orderunit = MapUtils.getString(bySql6.get(0), "orderunit");
                    String proid = MapUtils.getString(bySql6.get(0), "proid");
                    String taskid = MapUtils.getString(bySql6.get(0), "taskid");
                    String binnum = MapUtils.getString(bySql6.get(0), "binnum");
                    map1.put("unitcost", unitcost.toString());
                    map1.put("linecost", reamount.toString());
                    map1.put("binnum",binnum);
                    map1.put("proid", proid);
                    map1.put("unit",orderunit);
                    map1.put("taskid", taskid);
                }
                map1.put("prtdetaillineid",prtdetaillineid);//采购接收明细行id
                map1.put("prlid",prlid);//采购申请行id
                map1.put("polid",polid);//采购订单明细行id
                map1.put("pplid",pplid);//采购计划明细行id
                map1.put("receiptnum", receiptnum);
                map1.put("quantity", numbers);

                map1.put("type", "接收");
                map1.put("lotnum", batchnum);
                map1.put("subjectno", subjectno);
                List<Map<String, Object>> bySql5 = handlerService.findBySql("select ponum from gq_purchasereceipt where receiptnum ='" + receiptnum + "'");
                if(CollectionUtils.isNotEmpty(bySql5)){
                    map1.put("ponum", MapUtils.getString(bySql5.get(0), "ponum"));
                }
                handlerService.insert("gq_matusetrans", map1);

                //扫码入库成功后，对该采购接受行往该物料的历史价格放数据
                if(CollectionUtils.isNotEmpty(bySql)){
                    for (Map<String, Object> stringObjectMap : bySql) {
                        Map<String, Object> hispricematerMap = new HashMap<>();
                        String unitcost = MapUtils.getString(stringObjectMap, "unitcost");//采购接受行的单价
                        String itemnumNew = MapUtils.getString(stringObjectMap, "itemnum");//物资编码
                        String itemdescNew = MapUtils.getString(stringObjectMap, "itemdesc");//物资名称
                        String batchnumNew = MapUtils.getString(stringObjectMap, "batchnum");//批次号
                        String pronum = MapUtils.getString(stringObjectMap, "pronum");//项目号
                        String unit = MapUtils.getString(stringObjectMap, "unit");//订购单位
                        hispricematerMap.put("itemnum",itemnumNew);
                        hispricematerMap.put("itemdesc",itemdescNew);
                        hispricematerMap.put("batchnum",batchnumNew);
                        hispricematerMap.put("projectnum",pronum);
                        hispricematerMap.put("unitcost",unitcost);
                        hispricematerMap.put("orderunit",unit);
                        handlerService.insert("gq_hispricemater",hispricematerMap);
                    }
                }

                //扫码操作完成后，这个码只能扫一次，再扫一次就进行提示
                handlerService.findBySql("update gq_barcode set isscancode = 1 where itemnum = '" + itemnum + "' and batchnum = '" + batchnum + "'");

                prtidSet.add(MapUtils.getLong(map6,"id"));
            }
        }
        //扫码入库成功，修改采购接收里采购订单汇总行，采购订单里采购订单汇总行，
        if(CollectionUtils.isNotEmpty(prtidSet)){
            for (Long aLong : prtidSet) {
                // 处理采购接收申请接收逻辑
                dealReceiptPO1(aLong);
                //purchaseReceiptHandler.dealReceiptPO(aLong);
                // 更新供应商的到货率和合格率
                purchaseReceiptHandler.calculationPrPa(aLong);
            }
        }
      /*  List<Map<String, Object>> bySql = handlerService.findBySql("select id from gq_purchasereceipt where receiptnum ='" + receiptnum + "'");
        idt = Integer.toString((Integer) bySql.get(0).get("id"));
        result.put("idt",idt);*/
        code = "0";
        result.put("code", code);
        result.put("size", params.size());
        result.put("msg", message);
        return result;
    }

    private float InsertInventoryAllowanceCost(String receiptnum, String storenum, String itemnum, String batchnum, String itemdesc, String subjectno, String applyby,String occupnot, Map<String, Object> map3,String shelfnum) throws Exception {
        float numbers=0;
        Map<String, Object> map4 = new HashMap<>();
        String proid = MapUtils.getString(map3, "proid");
        map4.put("proid", proid);
        String taskid = MapUtils.getString(map3, "taskid");
        map4.put("taskid", taskid);
        String unitcost1 = MapUtils.getString(map3, "unitcost");
        BigDecimal bigDecimal = new BigDecimal(unitcost1);
        map4.put("stdcost", bigDecimal);
        map4.put("avgcost", bigDecimal);
        String linecost1 = MapUtils.getString(map3, "linecost");
        BigDecimal bigDecimal1 = new BigDecimal(linecost1);
        map4.put("lastcost", bigDecimal1);
        map4.put("itemnum", itemnum);
        map4.put("itemdesc", itemdesc);
        map4.put("storenum", storenum);
        map4.put("batchnum", batchnum);
        map4.put("subjectno", subjectno);
        handlerService.insert("gq_invcost", map4);
        //新建库存台账 余量子表
        Map<String, Object> map5 = new HashMap<>();
        map5.put("proid", proid);
        map5.put("taskid", taskid);
        numbers = MapUtils.getFloat(map3, "receiveqty");
        map5.put("avblbalance", numbers);
        //是本人占用   变更预留数量，不是本人占用  变更可用余量
        if("否".equals(MapUtils.getString(map3,"occupnot"))){

        }else{
            map5.put("reservedqty", numbers);
        }
        map5.put("curbal", numbers);
        String expirydate = MapUtils.getString(map3, "expirydate");
        if(StringUtils.isNotBlank(expirydate)){
            map5.put("enddate", expirydate);
            //根据失效日期和当前日期计算出失效天数
            int enddays = handlerExpirationDays(expirydate);
            map5.put("enddays", enddays);
        }

        map5.put("unitcost", bigDecimal);
        map5.put("itemnum", itemnum);
        map5.put("itemdesc", itemdesc);
        String orderunit = MapUtils.getString(map3, "orderunit");
        map5.put("orderunit",orderunit);//计量单位
        map5.put("storenum", storenum);
        map5.put("batchnum", batchnum);
        map5.put("receiptnum", receiptnum);
        map5.put("subjectno", subjectno);
        map5.put("binnum", MapUtils.getString(map3, "binnum"));
        if (occupnot.equals("是")){
            map5.put("applyby", applyby);
        }
        map5.put("binnum", shelfnum);
        String startdate =  DateUtiles.getYmdhms(new Date());
        map5.put("startdate",startdate);//入库时间
        handlerService.insert("gq_invbalances", map5);
        return numbers;
    }

    //根据失效日期距当前日期计算出失效天数
    private int handlerExpirationDays(String expirydate) {
        Date expirydate1 = DateUtils.getDate(expirydate);
        Date currentDate = DateUtils.getCurrentDate();
        int day = DateUtils.daysBetween(currentDate,expirydate1);
        if(day < 0){
            day = 0;
        }
        return day;
    }

    /* String storenum = hashMap2.get("storenum");
     String shelfnum = hashMap2.get("shelfnum");
     String itemnum = hashMap2.get("itemnum");
     String batchnum = hashMap2.get("batchnum");
     String receiptnum = hashMap2.get("receiptnum");
     if (storenum.isEmpty()&&shelfnum.isEmpty()){

     }else{
//                            List<Map<String, Object>> bySql1 = handlerService.findBySql("SELECT\n" +
////                                    "\ta.*,\n" +
////                                    "\tb.* \n" +
////                                    "FROM\n" +
////                                    "\tgq_purchasereceipt a\n" +
////                                    "\tLEFT JOIN gq_purchreceline b ON a.receiptnum = b.receiptnum \n" +
////                                    "WHERE\n" +
////                                    "\ta.receiptnum = '" + receiptnum + "'");
         Map<String, Object> instoMap = new HashMap<>();
         handlerService.addBizNum("instorageApp", "gq_instorage", instoMap);
         // 查询入库编码
         String instonum = (String) instoMap.get("instonum");
         List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_purchasereceipt where receiptnum ='" + receiptnum + "'");
         if (CollectionUtils.isNotEmpty(bySql1)){
             for (int j=0;j<bySql1.size();j++){
                 Map<String, Object> map1 = bySql1.get(j);
                 String receiptdesc = MapUtils.getString(map1, "receiptdesc");
                 String recipient = MapUtils.getString(map1, "recipient");
                 String type = MapUtils.getString(map1, "type");
                 String orgcode = MapUtils.getString(map1, "orgcode");
                 String deptcode = MapUtils.getString(map1, "deptcode");
                 String createtime = MapUtils.getString(map1, "createtime");
                 String createuserdesc = MapUtils.getString(map1, "createuserdesc");

                 HashMap<String, Object> hashMap = new HashMap<>();
                 hashMap.put("instonum",instonum);
                 hashMap.put("receiptnum",receiptnum);
                 hashMap.put("instodesc",receiptdesc);
                 hashMap.put("recipient",recipient);
                 hashMap.put("status","审批通过");
                 hashMap.put("type",type);
                 hashMap.put("orgcode",orgcode);
                 hashMap.put("createuserdesc",createuserdesc);
                 hashMap.put("createtime",createtime);
                 hashMap.put("deptcode",deptcode);
                 handlerService.insert("gq_instorage", hashMap);
                 List<Map<String, Object>> bySql2 = handlerService.findBySql("select * from gq_purchreceline  where receiptnum ='" + receiptnum + "'");
                 // 生成入库编码
                 if (CollectionUtils.isNotEmpty(bySql2)){
                     for (int i=0;i<bySql2.size();i++){
                         Map<String, Object> map4 = bySql2.get(i);
                          String ponum = MapUtils.getString(map1, "ponum");
                         String itemdesc = MapUtils.getString(map4, "itemdesc");
                         String linetype = MapUtils.getString(map4, "linetype");
                         Float orderqty = MapUtils.getFloat(map4, "orderqty");
                         String transactype = MapUtils.getString(map4, "transactype");
                         String model = MapUtils.getString(map4, "model");
                         String spemodel = MapUtils.getString(map4, "spemodel");
                         String manufacturer = MapUtils.getString(map4, "manufacturer");
                         String taskid = MapUtils.getString(map4, "taskid");
                         String proid = MapUtils.getString(map4, "proid");
                         ///// String batchnum1 = MapUtils.getString(map1, "batchnum");
                         String unit = MapUtils.getString(map4, "unit");
                         String outunit = MapUtils.getString(map4, "outunit");
                         String converfactor = MapUtils.getString(map4, "converfactor");
                         String expirydate = MapUtils.getString(map4, "expirydate");
                         String aactualdate = MapUtils.getString(map4, "aactualdate");
                         String accerequir = MapUtils.getString(map4, "accerequir");
                         String whepasaccep = MapUtils.getString(map4, "whepasaccep");
                         Float unitcost = MapUtils.getFloat(map4, "unitcost");
                         Float linecost = MapUtils.getFloat(map4, "linecost");
                         HashMap<String, Object> hashMap1 = new HashMap<>();
                         hashMap1.put("ponum",ponum);
                         hashMap1.put("itemnum", itemnum);
                         hashMap1.put("itemdesc",itemdesc);
                         hashMap1.put("linetype",linetype);
                         hashMap1.put("innum",orderqty);
                         hashMap1.put("issuetype",transactype);
                         hashMap1.put("model",model);
                         hashMap1.put("brand",spemodel);
                         hashMap1.put("manufacturer",manufacturer);
                         hashMap1.put("taskid",taskid);
                         hashMap1.put("proid",proid);
                         hashMap1.put("storageroom",storenum);
                         hashMap1.put("binnum",shelfnum);
                         hashMap1.put("batchnum",batchnum);
                         hashMap1.put("orderunit",unit);
                         hashMap1.put("unit",outunit);
                         hashMap1.put("converfactor",converfactor);
                         hashMap1.put("life",expirydate);
                         hashMap1.put("reqdeliverydate",aactualdate);
                         hashMap1.put("request",accerequir);
                         hashMap1.put("conforaccept",whepasaccep);
                         hashMap1.put("unitcost",unitcost);
                         hashMap1.put("linecost",linecost);
                         hashMap1.put("instonum",instonum);
                         handlerService.insert("gq_instoline",hashMap1);
                         List<Map<String, Object>> bySql = handlerService.findBySql("select id from gq_instorage where instonum ='" + instonum + "'");
                         idt = Integer.toString((Integer) bySql.get(0).get("id"));
                         result.put("idt",idt);
                     }
                 }
             }
         }

     }
     result.put("code",code);
     result.put("msg",msg);
     result.put("idt",idt);
     return result;
 }*/
    //返回采购订单，采购会总行数量金额
    public void dealReceiptPO1(Long id) {
        List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_prtdetailline where id='" + id + "'");
        if (CollectionUtils.isNotEmpty(bySql)){
            String prtslid = MapUtils.getString(bySql.get(0), "prtslid");//接收会总行id
            String prtid = MapUtils.getString(bySql.get(0), "prtid");//接收id
            String ppslid = MapUtils.getString(bySql.get(0), "ppslid");//采购计划汇总id
            List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_prtsummaryline where id='" + prtslid+"'");
            if (CollectionUtils.isNotEmpty(bySql1)){
                Map<String, Object> map = bySql1.get(0);
                Float number = MapUtils.getFloat(map, "number");//接受行入库数量
                Integer idt = MapUtils.getInteger(map, "id");//订单汇总行id
                Float receivedqty = MapUtils.getFloat(map, "receivedqty");//已接受数量
                Float unreceivedqty = MapUtils.getFloat(map, "unreceivedqty");//未接收数量
                Float qualiquan = MapUtils.getFloat(map, "qualiquan");//合格数量
                //未接受数量=未接受数量-入库数量
                float receivedqty1 = receivedqty + qualiquan;
                float unreceivedqty1 = unreceivedqty - qualiquan;
                if(unreceivedqty1<0.0f){
                    unreceivedqty1 = 0f;
                }
                handlerService.updateBySql("update gq_prtsummaryline set receiveqty =" + number + ",receivedqty=" + receivedqty1 + ",unreceivedqty=" + unreceivedqty1 + " where id=" + idt);

            }
            // 查询采购接收汇总行信息
            List<Map<String, Object>> prtslList = handlerService.findBySql("SELECT ponum,unitcost,prtid,receiveqty, poslid, ppslid,id,receivedqty,unreceivedqty,totalcost,qualquantity,qualiquan from gq_prtsummaryline where id = " + prtslid + " and receiveqty >0");
            BigDecimal decimal = new BigDecimal("0");
            String ponum="";
            if (CollectionUtils.isNotEmpty(prtslList)){
                for (Map<String, Object> prtslMap : prtslList) {
                    Integer prtslid2 = MapUtils.getInteger(prtslMap, "id");
                    Integer poslid = MapUtils.getInteger(prtslMap, "poslid");
                    Integer ppslid2 = MapUtils.getInteger(prtslMap, "ppslid");
                    Long prtid2 = MapUtils.getLong(prtslMap, "prtid");//主表id
                    Float receiveqty = MapUtils.getFloat(prtslMap, "qualiquan");//之前是按照入库数，现在改成按照合格数
                    Float receivedqty = MapUtils.getFloat(prtslMap, "receivedqty");
                    Float unreceivedqty = MapUtils.getFloat(prtslMap, "unreceivedqty");
                    Float qualquantity = MapUtils.getFloat(prtslMap, "qualquantity");//到货数量
                    Float totalcost = MapUtils.getFloat(prtslMap, "totalcost");//订单汇总行接收总额

                    Float unitcost = MapUtils.getFloat(prtslMap, "unitcost");//单价
                    ponum = MapUtils.getString(prtslMap, "ponum");
                    List<Map<String, Object>> polList = handlerService.findBySql("select receivedqty, orderqty, inspectqty,receivedtotalcost,qualquantity from gq_posline where id = " + poslid);
                    Map<String, Object> polMap = polList.get(0);
                    Float poslorderqty = MapUtils.getFloat(polMap, "orderqty");// 采购数量
                    Float poslreceivedqty = MapUtils.getFloat(polMap, "receivedqty");// 接收数量
                    Float poslinspectqty = MapUtils.getFloat(polMap, "inspectqty");// 验收数量
                    Float receivedtotalcost = MapUtils.getFloat(polMap, "receivedtotalcost");// 接收总额
                    Float qualquantity1 = MapUtils.getFloat(polMap, "qualquantity");//到货数量
// 计算数量
                    qualquantity1=qualquantity1-qualquantity;//到货数量
                    poslreceivedqty = poslreceivedqty + receiveqty;
                    Float poslunreceivedqty = poslorderqty - poslreceivedqty;
                    poslinspectqty = poslinspectqty + receiveqty;
                    Float posluninspectqty = poslorderqty - poslinspectqty;

                    receivedtotalcost = receivedtotalcost + totalcost;
                    String receiptscomplete = "已接收完成";
                    if (!poslorderqty.equals(poslreceivedqty)) {
                        receiptscomplete = "已完成部分接收";
                    }
                    handlerService.updateBySql("UPDATE gq_posline set receiptscomplete='" + receiptscomplete
                            + "', inspectqty = " + poslinspectqty + ", receivedqty =" + poslreceivedqty
                            + ", uninspectqty =" + posluninspectqty + ", unreceivedqty =" + poslunreceivedqty + ",receivedtotalcost = " + receivedtotalcost + ",qualquantity="+qualquantity1+"  where id = " + poslid);
                    List<Map<String, Object>> bySql2 = handlerService.findBySql("select receivedqty,unitcost from gq_posline where id = " + poslid);
                    if (CollectionUtils.isNotEmpty(bySql2)){
                        Map<String, Object> map = bySql2.get(0);
                        String receivedqty1 = MapUtils.getString(map, "receivedqty");
                        String unitcost1 = MapUtils.getString(map, "unitcost");
                        BigDecimal receivedqty2 = new BigDecimal(receivedqty1);
                        BigDecimal unitcost2 = new BigDecimal(unitcost1);
                        BigDecimal unpaidamts = new BigDecimal("0");
                        unpaidamts=receivedqty2.multiply(unitcost2);
                        handlerService.updateBySql("UPDATE gq_posline set unpaidamts='"+unpaidamts+"' where id = " + poslid);
                    }
                    handlerService.updateBySql("UPDATE gq_prtsummaryline set returnedqty = 0, returnableqty = " + poslreceivedqty + " where id = " + prtslid2);
                    // 处理采购订单明细行信息
                    //dealPODetail(prtid, prtslid);
                    dealPODetail(prtid, prtslid, ppslid,id);
                }
            }
            try {
                List<Map<String, Object>> bySql3 = handlerService.findBySql("select * from gq_posline where ponum='" + ponum + "'");
                if (CollectionUtils.isNotEmpty(bySql3)){
                    for (int i=0;i<bySql3.size();i++){
                        Map<String, Object> map = bySql3.get(i);
                        String receivedqty1 = MapUtils.getString(map, "receivedqty");
                        String unitcost1 = MapUtils.getString(map, "unitcost");
                        BigDecimal bigDecimal1 = new BigDecimal(unitcost1);
                        BigDecimal bigDecimal2 = new BigDecimal(receivedqty1);
                        BigDecimal paidamt = new BigDecimal("0");
                        paidamt = bigDecimal1.multiply(bigDecimal2);
                        //paidamt:累计已开票金额  //unpaidamt:待开票金额
                        decimal=decimal.add(paidamt);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            handlerService.updateBySql("update gq_po set unpaidamt ='"+decimal+"' where ponum = '"+ponum+"'");
        }
    }
    private void dealPODetail(String prtid, String prtslid, String ppslid,Long id) {
        String insresults = "合格";
        List<Map<String, Object>> prtdlList = handlerService.findBySql("SELECT receiveqty,qualiquan, polid, id,arrquantity from gq_prtdetailline where insresults='" + insresults + "' and id ='"+id+"'");
        if (CollectionUtils.isNotEmpty(prtdlList)){
            Map<String, Object> prtdlMap = prtdlList.get(0);
            Integer prtdlid = MapUtils.getInteger(prtdlMap, "id");
            Integer polid = MapUtils.getInteger(prtdlMap, "polid");
            Float prlorderqty = MapUtils.getFloat(prtdlMap, "qualiquan");//之前是以入库数更新采购订单里的数据，现在修改成合格数
            Float arrquantity = MapUtils.getFloat(prtdlMap, "arrquantity");
            // 查询采购订单行信息
            List<Map<String, Object>> polList = handlerService.findBySql("select receivedqty, orderqty, inspectqty,arrquantity from gq_poline where id = " + polid);
            Map<String, Object> polMap = polList.get(0);
            Float polorderqty = MapUtils.getFloat(polMap, "orderqty");// 采购数量
            Float polreceivedqty = MapUtils.getFloat(polMap, "receivedqty");// 接收数量
            Float polinspectqty = MapUtils.getFloat(polMap, "inspectqty");// 验收数量
            Float arrquantity1 = MapUtils.getFloat(polMap, "arrquantity");//到货数量
            // 计算数量
            polreceivedqty = polreceivedqty + prlorderqty;//总的接收数量（已接收+本次接收数量）
            polinspectqty = polinspectqty + prlorderqty;//总的验收数量（已验收+本次验收数量）
            Float poluninspectqty = polorderqty - polinspectqty;
            Float polunreceivedqty = polorderqty - polreceivedqty;//总的未接受数量（采购数量-总的接收数量）
            arrquantity1=arrquantity1-arrquantity;
            String receiptscomplete = "已接收完成";
            if (!polorderqty.equals(polreceivedqty)) {
                receiptscomplete = "已完成部分接受";
            }
            // 更新采购订单行数据
            handlerService.updateBySql("UPDATE gq_poline set receiptscomplete='" + receiptscomplete
                    + "', inspectqty = " + polinspectqty + ", receivedqty =" + polreceivedqty
                    + ", uninspectqty =" + poluninspectqty + ", unreceivedqty =" + polunreceivedqty + ",arrquantity="+arrquantity1+" where id = " + polid);

            // 更新采收接收行可退换数量
            handlerService.updateBySql("UPDATE gq_prtdetailline set returnedqty = 0, returnableqty = " + polreceivedqty + " where id = " + prtdlid);
        }
    }
    //扫码出库
    @PostMapping("/itemapply")
    public Map<String, Object> itemapplyWarehouse(@RequestBody List<Map<String, Object>> params) throws Exception {
        HashMap<String, Object> hashMap = new HashMap<>();
        HashMap<String, Object> data = new HashMap<>();
        String code = "";
        for (Map<String, Object> map : params) {
            //先扫库房信息，再扫物料信息
            String location = MapUtils.getString(map, "location");//库房编码
            String binnum = MapUtils.getString(map, "binnum");//货位
            String id = MapUtils.getString(map, "id");
            String itemnum = MapUtils.getString(map, "itemnum");//物料
            String lotnum = MapUtils.getString(map, "lotnum");//批次
            String itemapplynum = MapUtils.getString(map, "itemapplynum");//领用单编码

            //校验扫的库房码是不是该领用单的库房
            List<Map<String, Object>> bySql2 = handlerService.findBySql("select * from gq_itemapply where id='" + id + "' and location = '" + location + "'");
            if(CollectionUtils.isEmpty(bySql2)){
                String msg = "该领用单的库房编码不是"+location+", 请重新扫描库房信息!";
                hashMap.put("code", "1");
                hashMap.put("msg", msg);
                return hashMap;
            }
            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_itemapplyline where itemnum='" + itemnum + "' and lotnum='" + lotnum + "' and glnum='" + itemapplynum + "' and binnum='"+binnum+"'");
            if (CollectionUtils.isNotEmpty(bySql)) {
                for (int i = 0; i < bySql.size(); i++) {
                    Map<String, Object> map1 = bySql.get(i);
                    code = "0";
                    String msg = "该出库申请中存在此种物料信息！";
                    Integer id1 = MapUtils.getInteger(map1, "id");
                    String orunit = MapUtils.getString(map1, "Orunit");
                    Float avblbalance = MapUtils.getFloat(map1, "avblbalance");
                    Float quantity = MapUtils.getFloat(map1, "quantity");
                    Float grantqty = MapUtils.getFloat(map1, "GRANTQTY");
                    Float outquantity = MapUtils.getFloat(map1, "outquantity");//待出库数量
                    List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_item where itemnum='" + itemnum + "'");
                    String model = MapUtils.getString(bySql1.get(0), "model");
                    String outunit = MapUtils.getString(bySql1.get(0), "outunit");
                    String collectime = MapUtils.getString(map1, "collectime");
                    String pureason = MapUtils.getString(map1, "Pureason");
                    Float unitcost = MapUtils.getFloat(map1, "unitcost");
                    Float linecost = MapUtils.getFloat(map1, "linecost");
                    String binnum1 = MapUtils.getString(map1, "binnum");
                    String locations = MapUtils.getString(map1, "locations");
                    data.put("id", id1);
                    data.put("orunit", orunit);
                    data.put("avblbalance", avblbalance);
                    data.put("quantity", quantity);
                    data.put("grantqty", grantqty);
                    data.put("outquantity", outquantity);
                    data.put("model", model);
                    data.put("outunit", outunit);
                    data.put("collectime", collectime);
                    data.put("pureason", pureason);
                    data.put("unitcost", unitcost);
                    data.put("linecost", linecost);
                    data.put("binnum", binnum1);
                    data.put("locations", locations);
                    hashMap.put("code", code);
                    hashMap.put("data", data);
                    hashMap.put("msg", msg);
                }
            } else {
                code = "1";
                String msg = "该领料单的库房货位下没有此种物料信息！";
                hashMap.put("code", code);
                hashMap.put("msg", msg);
            }
        }
        return hashMap;
    }

    //扫码盘点
    @PostMapping("/check")
    public Map<String, Object> countWarehouse(@RequestBody List<Map<String, Object>> params) throws Exception {
        HashMap<String, Object> hashMap = new HashMap<>();
        HashMap<String, Object> data = new HashMap<>();
        for (Map<String, Object> map : params) {
            //先扫库房信息，再扫物料信息
            String storenum = MapUtils.getString(map, "storenum");//库房编码
            String shelfnum = MapUtils.getString(map, "shelfnum");//货位
            String id = MapUtils.getString(map, "id");
            String itemnum = MapUtils.getString(map, "itemnum");//物料
            String batchnum = MapUtils.getString(map, "batchnum");//批次

            //校验扫的库房码是不是该盘点单的库房
            List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_check where id='" + id + "' and kid = '" + storenum + "'");
            if(CollectionUtils.isEmpty(bySql1)){
                String msg = "该库存盘点的库房编码不是"+storenum+", 请重新扫描库房信息!";
                hashMap.put("code", "1");
                hashMap.put("msg", msg);
                return hashMap;
            }

            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_kcpdzb where itemnum='" + itemnum + "' and lotnum='" + batchnum + "' and pidd='" + id + "' and binnum ='" + shelfnum+"'");
            if (CollectionUtils.isNotEmpty(bySql)) {
                for (int i = 0; i < bySql.size(); i++) {
                    Map<String, Object> map1 = bySql.get(i);
                    Integer id1 = MapUtils.getInteger(map1, "id");
                    String itemname = MapUtils.getString(map1, "ITEMNAME");
                    Float inventory = MapUtils.getFloat(map1, "Inventory");
                    String checker = MapUtils.getString(map1, "checker");
                    String counnot = MapUtils.getString(map1, "counnot");
                    String invtime = MapUtils.getString(map1, "Invtime");
                    Float unitcost = MapUtils.getFloat(map1, "unitcost");
                    String binnum = MapUtils.getString(map1, "binnum");
                    Float checkqty = MapUtils.getFloat(map1, "checkqty");
                    Float linecost = MapUtils.getFloat(map1, "linecost");
                    String proid = MapUtils.getString(map1, "proid");
                    String taskid = MapUtils.getString(map1, "taskid");
                    data.put("id", id1);
                    data.put("itemname", itemname);
                    data.put("inventory", inventory);
                    data.put("checker", checker);
                    data.put("counnot", counnot);
                    data.put("invtime", invtime);
                    data.put("unitcost", unitcost);
                    data.put("binnum", binnum);
                    data.put("checkqty", checkqty);
                    data.put("linecost", linecost);
                    data.put("proid", proid);
                    data.put("taskid", taskid);
                    String msg = "该库存盘点中存在此种物料信息！";
                    hashMap.put("code", "0");
                    hashMap.put("data", data);
                    hashMap.put("msg", msg);
                }
            } else {
                String msg = "该库存盘点库房货位下没有此种物料信息！";
                hashMap.put("code", "1");
                hashMap.put("msg", msg);
            }
        }
        return hashMap;
    }

    //入库申请，判断扫码找对应唯一子表
    @PostMapping("/instorage")
    public Map<String, Object> instorage(@RequestBody List<Map<String, Object>> params) throws Exception {
        HashMap<String, Object> hashMap = new HashMap<>();
        HashMap<String, Object> data = new HashMap<>();
        for (Map<String, Object> map : params) {
            String id = MapUtils.getString(map, "id");
            String itemnum = MapUtils.getString(map, "itemnum");//物料
            String batchnum = MapUtils.getString(map, "batchnum");//批次
            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_instoline where itemnum='" + itemnum + "' and batchnum='" + batchnum + "' and instonum in (select instonum from gq_instorage where id ='"+id+"')");
            if (CollectionUtils.isNotEmpty(bySql)) {
                for (int i = 0; i < bySql.size(); i++) {
                    Map<String, Object> map1 = bySql.get(i);
                    data.put("id", MapUtils.getInteger(map1,"id"));
                    data.put("itemnum", MapUtils.getString(map1,"itemnum"));
                    data.put("itemname", MapUtils.getString(map1,"itemdesc"));
                    data.put("binnum", MapUtils.getString(map1,"binnum"));
                    data.put("model", MapUtils.getString(map1,"model"));
                    data.put("orderunit", MapUtils.getString(map1,"orderunit"));
                    data.put("innum", MapUtils.getString(map1,"innum"));
                    data.put("unitcost", MapUtils.getString(map1,"unitcost"));
                    data.put("linecost", MapUtils.getString(map1,"linecost"));
                    data.put("batchnum", MapUtils.getString(map1,"batchnum"));
                    data.put("reqdeliverydate", MapUtils.getString(map1,"reqdeliverydate"));
                    data.put("occupnot", MapUtils.getString(map1,"occupnot"));
                    data.put("life", MapUtils.getString(map1,"life"));
                    String msg = "该入库申请单中存在此种物料信息！";
                    hashMap.put("code", "0");
                    hashMap.put("data", data);
                    hashMap.put("msg", msg);
                }
            } else {
                String msg = "该入库申请单中没有此种物料信息！";
                hashMap.put("code", "1");
                hashMap.put("msg", msg);
            }
        }
        return hashMap;
    }


    //退库 校验条码
    @PostMapping("/returnsStore")
    public Map<String, Object> returnsStore(@RequestBody List<Map<String, Object>> params) throws Exception {
        HashMap<String, Object> hashMap = new HashMap<>();
        for (Map<String, Object> map : params) {
            //先扫库房信息，再扫物料信息
            String location = MapUtils.getString(map, "location");//库房编码
            String binnum = MapUtils.getString(map, "binnum");//货位
            String id = MapUtils.getString(map, "id");
            String returnnum = MapUtils.getString(map, "returnnum");//退库编码
            String itemnum = MapUtils.getString(map, "itemnum");//物料
            String tolot = MapUtils.getString(map, "tolot");//批次

            //校验扫的库房码是不是该盘点单的库房
            List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_return where id='" + id + "' and location = '" + location + "'");
            if(CollectionUtils.isEmpty(bySql1)){
                String msg = "该库存盘点的库房编码不是"+location+", 请重新扫描库房信息!";
                hashMap.put("code", "1");
                hashMap.put("msg", msg);
                return hashMap;
            }

            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_returnline where itemnum='" + itemnum + "' and tolot='" + tolot + "' and invusenum='" + returnnum + "' and binnum ='" + binnum+"'");
            if (CollectionUtils.isNotEmpty(bySql)) {
                for (int i = 0; i < bySql.size(); i++) {
                    Map<String, Object> map1 = bySql.get(i);
                    ObjectMapper mapper = new ObjectMapper();
                    String s1 = mapper.writeValueAsString(map1);
                    Map<String, String> stringStringMap = mapper.readValue(s1, new TypeReference<Map<String, String>>() {
                    });
                    String msg = "该退库单中存在此种物料信息！";
                    hashMap.put("code", "0");
                    hashMap.put("data", stringStringMap);
                    hashMap.put("msg", msg);
                }
            } else {
                String msg = "该退库单库房货位下没有此种物料信息！";
                hashMap.put("code", "1");
                hashMap.put("msg", msg);
            }
        }
        return hashMap;
    }

    //报废 校验条码
    @PostMapping("/scraplicationScan")
    public Map<String, Object> scraplicationScan(@RequestBody List<Map<String, Object>> params) throws Exception {
        HashMap<String, Object> hashMap = new HashMap<>();
        for (Map<String, Object> map : params) {
            //先扫库房信息，再扫物料信息
            String warehouse = MapUtils.getString(map, "warehouse");//库房编码
            String location = MapUtils.getString(map, "location");//货位
            String id = MapUtils.getString(map, "id");
            String scraption = MapUtils.getString(map, "scraption");//报废单号
            String matcode = MapUtils.getString(map, "matcode");//物料
            String batch = MapUtils.getString(map, "batch");//批次

            //校验扫的库房码是不是该盘点单的库房
            List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_scraplication where id='" + id + "' and warehouse = '" + warehouse + "'");
            if(CollectionUtils.isEmpty(bySql1)){
                String msg = "该报废申请的库房编码不是"+warehouse+", 请重新扫描库房信息!";
                hashMap.put("code", "1");
                hashMap.put("msg", msg);
                return hashMap;
            }

            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_Listscrapped where matcode='" + matcode + "' and batch='" + batch + "' and scraption='" + scraption + "' and location ='" + location+"'");
            if (CollectionUtils.isNotEmpty(bySql)) {
                for (int i = 0; i < bySql.size(); i++) {
                    Map<String, Object> map1 = bySql.get(i);
                    ObjectMapper mapper = new ObjectMapper();
                    String s1 = mapper.writeValueAsString(map1);
                    Map<String, String> stringStringMap = mapper.readValue(s1, new TypeReference<Map<String, String>>() {
                    });
                    String msg = "该报废申请中存在此种物料信息！";
                    hashMap.put("code", "0");
                    hashMap.put("data", stringStringMap);
                    hashMap.put("msg", msg);
                }
            } else {
                String msg = "该报废申请的库房货位下没有此种物料信息！";
                hashMap.put("code", "1");
                hashMap.put("msg", msg);
            }
        }
        return hashMap;
    }



}
